
import { sys } from "cc";
import { FObject } from "../../../../[框架]/Core/模型/FObject";
import TaskProxy from "../../Controller/TaskProxy";
import { TaskDefine } from "../TaskDefine";


export class ADPlayRequest extends FObject implements TaskDefine.IAdPlayRequest {

    adUnitId: string = "";
    adStartTime: number = 0;
    requiresForcedClick: boolean = false;//是否需要强点
    status: TaskDefine.RequestStatus = "playing";
    private _forceClickTime: number = 0;//强制点击了多久
    adEndTime: number = 0;
    desc: string = null;


    get hasForcedClickCompleted(): boolean {
        if (!this.adCompleted) {
            Guyu.Log.warn(`广告没有看完`);
            return false;
        }
        let res = false;
        let taskConfig = TaskProxy.inst.taskConfig;
        //秒
        let needTime = Number(taskConfig.force_click_time) + Number(taskConfig.video_play_time);
        let needForceClickTimeMS = Number(taskConfig.force_click_time) * 1000;
        //毫秒
        needTime *= 1000;
        //如果是安卓 只用判断强点时间
        let onAndroid = sys.os === sys.OS.ANDROID;
        if (onAndroid) {
            res = this.forceClickTime >= needForceClickTimeMS;
            Guyu.Log.warn(`强点时长=${this.forceClickTime},需要时长=${needForceClickTimeMS}, 达成:${res}`);
            return res;
        }

        //iOS 播放完广告时间+停留时间 >广告时长+需要强点的时间，判定为已经强点，否则提示用户需要点击广告体验
        res = this.allCostTime >= needTime;
        Guyu.Log.warn("ADPlayRequest", "hasForcedClickCompleted", res, this.allCostTime, needTime);
        return res;
    }

    setStatus(status: TaskDefine.RequestStatus) {
        this.status = status;
    }

    get adCompleted() {
        return this.status == "completed";
    }

    updateEndTime() {
        this.adEndTime = Date.now();
    }

    updateStartTime() {
        this.adStartTime = Date.now();
    }

    updateForceClickTime(time: number) {
        if (time >= this._forceClickTime) {
            this._forceClickTime = time;
            Guyu.Log.warn(`更新强点时间=${time}ms`);
        }
    }

    //如果是iOS  forceClickTime 包含于 costTime
    get forceClickTime(): number {
        return this._forceClickTime;
    }


    //总消耗时间，如果是安卓 costTime = 广告时长 + forceClickTime(实际强制点击的时间)
    //如果是iOS  costTime = 播放广告回调前后时间 相当于是 （广告时长 + 强击事件）
    get allCostTime(): number {
        if (this.status != "completed") {
            return 0;
        }
        let diff = this.adEndTime - this.adStartTime;
        return diff;
    }

    //是否可以给奖励
    get canGiveReward(): boolean {
        let res = this.adCompleted;
        if (this.requiresForcedClick) {
            res = this.adCompleted && this.hasForcedClickCompleted;
        }
        return res;
    }
}